<?php
// https://leetcode-cn.com/problems/merge-sorted-array/
class Solution {

    /**
     * @param Integer[] $nums1
     * @param Integer $m
     * @param Integer[] $nums2
     * @param Integer $n
     * @return NULL
     */
    function merge(&$nums1, $m, $nums2, $n) {
        $nums1_copy = $nums1;
        $p = $p1 = $p2 = 0;

        while ($p1 < $m && $p2 < $n) {
            $nums1[$p++] = $nums1_copy[$p1] > $nums2[$p2] ? $nums2[$p2++] : $nums1_copy[$p1++];
        }

        if ($p1 < $m) {
           for (; $p1<$m; $p1++) {
               $nums1[$p++] = $nums1_copy[$p1];
           }
        }

        if ($p2 < $n) {
            for (; $p2<$n; $p2++) {
                $nums1[$p++] = $nums2[$p2];
            }
        }
    }
}